


/**
 *  返回图片动态地址
 *
 * @param  imagename 文件名
 **/
export function autoimg(imagename) {
    let re = new URL(`../assets/images/${imagename}`, import.meta.url).href
    return re

}

/**
 *  返回背景图动态地址
 *
 * @param  imagename 文件名
 **/
export function backgroundImg(imagename, position = '50% 100%', backgroundsize = 'cover') {
    let re = `background: url('${autoimg(imagename)}') no-repeat ${position};background-size: ${backgroundsize};`
    return re
}



const modules = import.meta.glob('@/components/**/**.vue');
/**
 *  获取动态组件
 *
 * @param  comName 组件名
 **/
export function getComponents(comName) {
    let _link = {}
    for (let key in modules) {
        let filename = key.split('/')[key.split('/').length - 1].split('.vue')[0];
        if (filename === comName)
            _link = modules[key]
    }

    return markRaw(defineAsyncComponent(_link))
}

/**
 *  动态读取文件内容
 *
 * @param  filename 名
 **/
export function readTestFile(filename) {

    const file = loadFile(`code/${filename}`)
    return unicodeToUtf8(file)
}

// 读取文件

function loadFile(name) {

    const xhr = new XMLHttpRequest()

    const okStatus = document.location.protocol === 'file:' ? 0 : 200

    xhr.open('GET', name, false)

    xhr.overrideMimeType('text/html;charset=utf-8')

    // 默认为utf-8

    xhr.send(null)

    return xhr.status === okStatus ? xhr.responseText : null
}

// unicode转utf-8

function unicodeToUtf8(data) {

    data = data.replace(/\\/g, '%')

    return unescape(data)
}


// 日期格式化
export function parseTime(time, pattern) {
    if (arguments.length === 0 || !time) {
        return null
    }
    const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    // console.log(time)   //2023-02-18T00:00:00.000+08:00
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time)
        } else if (typeof time === 'string' && time.indexOf('T') > 0) {
            date = new Date(time)
        } else {
            if (typeof time === 'string') {
                time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
            }
            if ((typeof time === 'number') && (time.toString().length === 10)) {
                time = time * 1000
            }
            date = new Date(time)   //火狐浏览器new Date有问题,只支持 01/23/2023 18:09:12
        }
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}

export function dateStrToTimeStamp(date) {
    date = date.substring(0, 19);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime();
    return timestamp
}

export function dateFormat(daterc) {
    // const daterc = row[column.property]
    if (daterc != null) {
        var date = new Date(daterc);
        var year = date.getFullYear();
        /* 在日期格式中，月份是从0开始，11结束，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        // 拼接
        return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
    }
}

export function formatDuring(mss) {
    var hours = parseInt(mss / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = parseInt((mss % (1000 * 60)) / 1000);
    var str =
        (hours) +
        "时" +
        (minutes) +
        "分"
    // +
    // (seconds) +
    // "秒";
    return str;
};
